import http from '@/server/request.js';
import * as Utils from '@/utils/index.js';
import * as Auth from '@/utils/auth';

let header = { 'content-type': 'application/json' };
let token_type = 'bearer';
let acc_token = '';
let acc_token_header_key = 'Authorization';

/**
 * 已经集齐卡人数和开奖时间
 */
export const getConfig = async () => {
  const data = await http.request({
    url: '/pass/godOfWealthCard/getConfig',
    method: 'GET',
  });
  if (!data.data.success) {
    uni.showToast({
      title: data.data.message || '获取卡信息失败',
      duration: 2000,
      icon: 'none',
    });
    return Promise.reject(new Error(data.data.message || '获取卡信息失败'));
  }
  return data;
};

/**
 * 查询几种卡类型
 */
export const getCardType = async () => {
  const data = await http.request({
    url: '/pass/godOfWealthCard/getCardType',
    method: 'GET',
  });
  if (!data.data.success) {
    uni.showToast({
      title: data.data.message || '获取卡信息失败',
      duration: 2000,
      icon: 'none',
    });
    return Promise.reject(new Error(data.data.message || '获取卡信息失败'));
  }
  return data;
};

/**
 * 中奖名单
 */
export const pagePrize = async (param) => {
  const data = await http.request({
    url: '/pass/godOfWealthCard/pagePrize',
    method: 'GET',
    data: param,
  });
  if (!data.data.success) {
    uni.showToast({
      title: data.data.message || '获取中奖名单失败',
      duration: 2000,
      icon: 'none',
    });
    return Promise.reject(new Error(data.data.message || '获取中奖名单失败'));
  }
  return data;
};

/**
 * 自己的中奖奖品
 */
export const selfPrize = async () => {
  let header = {
    auth: true,
  };
  const data = await http.request({
    url: '/api/godOfWealthCard/selfPrize',
    method: 'GET',
    header,
  });
  if (!data.data.success) {
    uni.showToast({
      title: data.data.message || '获取我的奖品信息失败',
      duration: 2000,
      icon: 'none',
    });
    return Promise.reject(new Error(data.data.message || '获取我的奖品信息失败'));
  }
  return data;
};

/**
 * 查询已合成的卡和已集齐的卡
 */
export const getCard = async () => {
  let header = {
    auth: true,
  };
  const data = await http.request({
    url: '/api/godOfWealthCard/getCard',
    method: 'GET',
    header,
  });
  if (!data.data.success) {
    uni.showToast({
      title: data.data.message || '获取我的卡信息失败',
      duration: 2000,
      icon: 'none',
    });
    return Promise.reject(new Error(data.data.message || '获取我的卡信息失败'));
  }
  return data;
};

/**
 * 查询本日还可翻卡次数
 */
export const getFlipSum = async () => {
  let header = {
    auth: true,
  };
  const data = await http.request({
    url: '/api/godOfWealthCard/getFlipSum',
    method: 'GET',
    header,
  });
  if (!data.data.success) {
    uni.showToast({
      title: data.data.message || '获取我的翻卡次数信息失败',
      duration: 2000,
      icon: 'none',
    });
    return Promise.reject(new Error(data.data.message || '获取我的翻卡次数信息失败'));
  }
  return data;
};

/**
 * 翻卡
 */
export const flipCard = async (param) => {
  let header = {
    auth: true,
    encryption: true,
  };
  const data = await http.request({
    url: `/api/godOfWealthCard/flipCard`,
    method: 'POST',
    header,
    data: param,
  });
  if (!data.data.success) {
    uni.showToast({
      title: data.data.message || '翻卡失败',
      duration: 2000,
      icon: 'none',
    });
    return Promise.reject(new Error(data.data.message || '翻卡失败'));
  }
  return data;
};

/**
 * 合成卡
 */
export const composeCard = async (param) => {
  let header = {
    auth: true,
    encryption: true,
  };
  const data = await http.request({
    url: `/api/godOfWealthCard/composeCard`,
    method: 'POST',
    header,
    data: param,
  });
  if (!data.data.success) {
    uni.showToast({
      title: data.data.message || '合成卡失败',
      duration: 2000,
      icon: 'none',
    });
    return Promise.reject(new Error(data.data.message || '合成卡失败'));
  }
  return data;
};
